package com.kly.agent.app;

import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.kly.agent.advisor.LoggingAdvisor;
import com.kly.agent.advisor.ReReadingAdvisor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @author admin
 * @date 2025/5/19
 */
@Component
@Slf4j
public class LoveApp {

    private final ChatClient chatClient;

    @Resource
    private RedisTemplate<String, Message> redisTemplate;



    public LoveApp(DashScopeChatModel dashScopeChatModel) {
        String SYSTEM_PROMPT = ResourceUtil.readUtf8Str("prompt/love-prompt.txt");
        SystemPromptTemplate promptTemplate = new SystemPromptTemplate(SYSTEM_PROMPT);

        Map<String, Object> systemPromptMap = new HashMap<>();
        systemPromptMap.put("name", "恋爱心理领域");
        systemPromptMap.put("method", "解决方案");
        systemPromptMap.put("otherRequest", "每次对话后生成title, 内容为建议列表");

        String render = promptTemplate.render(systemPromptMap);

        // 基于内存
        // InMemoryChatMemory chatMemory = new InMemoryChatMemory();
        // 基于文件
        String filePath = System.getProperty("user.dir") + "/temp/chat-memory";
        FileBasedChatMemory fileBasedChatMemory = new FileBasedChatMemory(filePath);
        this.chatClient = ChatClient.builder(dashScopeChatModel)
                .defaultSystem(render)
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(fileBasedChatMemory),
                        // 自定义日志 advisor
                        new LoggingAdvisor(),
                //         重复读取
                        new ReReadingAdvisor()
                )
                .build();
    }

    public void doChat(String question, String conversantId) {
        ChatResponse response = chatClient
                .prompt()
                .user(question)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, conversantId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 3))
                .call()
                .chatResponse();
        String content = response.getResult().getOutput().getContent();
        System.out.println("content = " + content);
    }


    record LoveReport(String title, List<String> contents) {}

    public LoveReport getLoveReport(String question, String conversantId) {
        return chatClient.prompt()
                .user(question)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, conversantId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .entity(LoveReport.class);
    }


}
